#ifndef ENGINE_APP_STATE_TASK_H
#define ENGINE_APP_STATE_TASK_H

#include "boost/shared_ptr.hpp"

#include "engine/task.h"
#include "engine/application_state.h"
#include "engine/window_wrapper.h"
#include "engine/kernel.h"

namespace engine{

class AppStateTask : public Task {
public:  
    AppStateTask(boost::shared_ptr<WindowWrapper> window) : window_(window) {}
    inline void SetWindowHandle(boost::shared_ptr<WindowWrapper> & window){window_ = window;}
    inline boost::shared_ptr<WindowWrapper> GetWindowHandle() const {return window_; }
    inline const ApplicationState & GetApplicationState() const {return window_->GetApplicationState();}
    
    int Start() {
        if(!window_->GetApplicationState().test(APP_INITIALIZED))
            return window_->Initialize();
        else return base::OK_OK;       
    }

    int Update() {
        int result = 0;

        result |= window_->DoEvents();

        if(window_->GetApplicationState().test(APP_CLOSED))
            result |= Kernel::KillAllTasks();
        else result |= base::OK_FALSE;

        return result;    
    }

    int Stop() {
        int result = 0;
        result |= window_->Close();
        result |= Kernel::KillAllTasks();
        return result;
    }


    int OnSuspend() {
        return window_->ReleaseResources();
    }

    int OnResume() {
        return window_->CaptureResources();
    }
private:
    boost::shared_ptr<WindowWrapper> window_;
    DISALLOW_IMPLICIT_CONSTRUCTORS(AppStateTask);
};

}

#endif //ENGINE_APP_STATE_TASK_H